import { _decorator, Component, Node, Prefab, instantiate, Vec2, Vec3, Size, view, UITransform, math, v3, director, randomRange, randomRangeInt, find } from 'cc';
import { enemy } from './enemy';
const { ccclass, property } = _decorator;

/**
 * 敌机参数
 */
class EnemyParam {
    speed: number;
    hp: number;
    score: number;
    constructor(speed: number, hp: number, score: number) {
        this.speed = speed
        this.hp = hp
        this.score = score
    }
}

@ccclass('EnemyFactory')
export class EnemyFactory extends Component {

    // 游戏时间
    gameTime: number = 0;


    // 敌机
    @property(Prefab)
    enemy1: Prefab;

    @property(Prefab)
    enemy2: Prefab;

    @property(Prefab)
    enemy3: Prefab;

    // 敌机父节点
    enemys: Node;

    /**
     * 敌机基础参数常量
     */
    enemyParams: EnemyParam[] = [
        new EnemyParam(4 * 3, 10, 100),
        new EnemyParam(2 * 3, 30, 300),
        new EnemyParam(1 * 2, 60, 600)
    ]

    // 最优游戏时间 s
    bestTime: number = 120;

    // 每秒最多生成敌人
    enemyRate: number = 4;



    viewSize: Size;

    start() {
        this.viewSize = view.getVisibleSize()

        this.enemys = find('Canvas/enemys')

        this.startGame()
    }

    update(deltaTime: number) {

        if (this.gameTime < this.bestTime) {
            this.gameTime += deltaTime
        }


        // console.log('gameTime', this.gameTime);
        // console.log('deltaTime', deltaTime);

    }



    /**
     * 
     * 开始游戏  开始生成敌人
     * 
     */
    startGame() {

        /**
         * 
         * 设定游戏的最佳时长为60s, 60s敌机生成和移动速度达到巅峰
         * 
         * 速度 1s 生成 1-n个敌人,  n = 6
         * 敌机的速度随着时间的增加而变快, 最多不超过2倍
         * 每次生成敌人 enemy1 2 3 的概率依次为 4 2 1
         * 
         */


        this.schedule(() => {
            let n = Math.floor((this.gameTime / this.bestTime) * (this.enemyRate - 1) + 1)
            // console.log('generate enemy:', n);

            for (let i = 0; i < n; i++) {
                this.randomGenerate()
            }
        }, 1)

    }



    /**
     * 
     * 随机生成敌机
     * 
     */
    randomGenerate() {
        // 生成敌人 enemy1 2 3 的概率依次为 4 2 1
        let r = randomRange(0, 7)
        
        // console.log('r: ', r);
        
        if (r >= 0 && r <= 5) {
            // 1
            this.generate(this.enemy1, this.getCurEnemyParam(0))
        } else if (r > 5 && r <= 6.8) {
            // 2
            this.generate(this.enemy2, this.getCurEnemyParam(1))
        } else if (r > 6.8 && r < 7) {
            // 3
            this.generate(this.enemy3, this.getCurEnemyParam(2))
        }
    }


    /**
     * 生成敌机
     */
    generate(enemyPrefab: Prefab, param: EnemyParam) {
        let en = instantiate(enemyPrefab)

        let enemyScript = en.getComponent(enemy)
        enemyScript.hp = param.hp
        enemyScript.score = param.score
        enemyScript.speed = param.speed

        // en.setParent(director.getScene().getChildByName('Canvas'))
        // director.getScene().getChildByName('Canvas').addChild(en)
        // director.getScene().getChildByName('Canvas').insertChild(en, 2)
        en.setParent(this.enemys)

        en.setPosition(this.getRandomPositon(en.getComponent(UITransform).width))
    }


    /**
     * 获取当前敌机参数
     */
    getCurEnemyParam(index: number) {
        let tmpEnemyPara = this.enemyParams[index]
        // 计算当前速度
        let speed = Math.ceil((this.gameTime / this.bestTime + 1) * tmpEnemyPara.speed)

        // console.log('time: ', this.gameTime, '--', speed + '---' + index);

        return new EnemyParam(tmpEnemyPara.speed, tmpEnemyPara.hp, tmpEnemyPara.score)
    }



    /**
     * 获取一个随机位置
     * @param width 
     * @returns 
     */
    getRandomPositon(width: number = 100) {
        // x 不超过可攻击范围
        let tmp = this.viewSize.width / 2 - width / 2
        return v3(math.randomRange(-tmp, tmp), 450 + randomRangeInt(0, 50))
    }



}


